package com.wanmait.baohan.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.mapper.RoleMapper;
import com.wanmait.baohan.mapper.WorkTemplateContentMapper;
import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.InitiateProjectService;
import com.wanmait.baohan.service.WorkTemplateContentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2021/2/12 21:18
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkTemplateContentServiceImpl implements WorkTemplateContentService {

    @Resource
    private WorkTemplateContentMapper workTemplateContentMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private InitiateProjectService initiateProjectService;

    @Override
    public void insert(WorkTemplateContent object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(WorkTemplateContent object) {

    }

    @Override
    public WorkTemplateContent findById(Integer id) {
        return null;
    }

    @Override
    public List<WorkTemplateContent> findAll() {
        return null;
    }

    @Override
    public List<WorkTemplateContent> findAll(WorkTemplateContent object) {
        return null;
    }

    @Override
    public PageInfo<WorkTemplateContent> findPage(int pageNum) {
        return null;
    }

    @Override
    public PageInfo<WorkTemplateContent> findPage() {
        return null;
    }

    @Override
    public PageInfo<WorkTemplateContent> findPage(int pageNum, WorkTemplateContent object) {
        return null;
    }

    @Override
    public PageInfo<WorkTemplateContent> findPage(WorkTemplateContent object) {
        return null;
    }

    @Override
    public List<WorkTemplateContent> findByWorkTemplateId(Integer id) {
        QueryWrapper<WorkTemplateContent> workTemplateContentQueryWrapper = new QueryWrapper<>();
        workTemplateContentQueryWrapper.eq("work_template_id", id).orderByAsc("id");
        List<WorkTemplateContent> workTemplateContents = workTemplateContentMapper.selectList(workTemplateContentQueryWrapper);
        workTemplateContents.forEach(workTemplateContent -> {
            if (workTemplateContent.getRoleId() != null) {
                workTemplateContent.setRoleName(roleMapper.selectByPrimaryKey(workTemplateContent.getRoleId()).getName());
            }
        });
        return workTemplateContents;
    }

    @Override
    public void updateTemplateContent(WorkTemplateContent workTemplateContent) {
        workTemplateContentMapper.updateById(workTemplateContent);
    }

    @Override
    public void insertTemplateContent(WorkTemplateContent workTemplateContent) {
        workTemplateContentMapper.insert(workTemplateContent);
    }

    @Override
    public List<WorkTemplateExecutable> createWorkTemplateExecutableByWorkTemplate(Integer id) {
        List<WorkTemplateContent> workTemplateContents = findByWorkTemplateId(id);
        ArrayList<WorkTemplateExecutable> workTemplateExecutables = new ArrayList<>();
        workTemplateContents.forEach(workTemplateContent -> {
            WorkTemplateExecutable workTemplateExecutable = new WorkTemplateExecutable();
            workTemplateExecutable.setWorkTemplateContent(workTemplateContent);
            workTemplateExecutables.add(workTemplateExecutable);
            workTemplateExecutable.setStaff(new Staff());
        });
        return workTemplateExecutables;
    }
}
